package gui;


import java.util.Iterator;

import javax.swing.table.AbstractTableModel;

import database.Branch;
import database.Database;
import database.DatabaseManager;
import database.Release;
import database.Package;
import database.ReleaseIdentifier;


/**
 * @author aioros
 * 
 * Abstract packages table
 */
public class PackagesTableModel extends AbstractTableModel{

	/*-------------------- Fields ------------------------*/
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * columns name
	 */
	private String[] columns = { "Name", "Type", "Description", "Branch Name", "Version", "State", "Category", "Install" };
	
	/**
	 * Database xml
	 */
	private Database database;
	
	
	/*-------------------- Constructor ------------------------*/

	/**
	 * Build a Packages Table Model
	 */
	public PackagesTableModel() {
		
		database = DatabaseManager.getDatabaseManager().getLocalDatabase();
	}
	
	
	
	/*-------------------- Methodes ------------------------*/
	
	/**
	 * @return Count Column
	 */
	public int getColumnCount() {
		return this.columns.length;
	}

	
	/**
	 * @return Count Row
	 */
	public int getRowCount() {
		
		int size = 0;
		
		Iterator<Package> itPkg = this.database.getPackages().values().iterator();
		Package pkg = null;
		Iterator<Branch> itBch = null;
		Branch bch = null;
		Iterator<Release> itRls = null;
		Release rls = null;
		
		
		while(itPkg.hasNext()){
			
			pkg = itPkg.next();
			itBch = pkg.getBranches().values().iterator();
			while(itBch.hasNext()){
				
				bch = itBch.next();
				itRls = bch.getReleases().values().iterator();
				while(itRls.hasNext()){
					
					rls = itRls.next();
					if(rls.getStatus().equals("active") || rls.getStatus().equals("deprecated") || rls.getInstall().equals("installed") ){
						size++;
					}
				}
			}
		}
		return size;
	}

	
	/**
	 * @return Column Name
	 */
	public String getColumnName(int column) {
		return columns[column];
	}
	
	
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
	
	
	public boolean isCellEditable(int row, int col){
		
		if (col == 0){
			
			return true;
		}
		return false;
	}
	
	
	/**
	 * @return cell value associate to rowIndex and columnIndex
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		
		Object result = null;
		
		
		/* --------- */

		int i = rowIndex + 1;
		
		Iterator<Package> itPkg = this.database.getPackages().values().iterator();
		Package pkg = null;
		Iterator<Branch> itBch = null;
		Branch bch = null;
		Iterator<Release> itRls = null;
		Release rls = null;
		
		
		while(itPkg.hasNext() && (i > 0)){
			
			pkg = itPkg.next();
			itBch = pkg.getBranches().values().iterator();
			while(itBch.hasNext() && (i > 0)){
				
				bch = itBch.next();
				itRls = bch.getReleases().values().iterator();
				while(itRls.hasNext() && (i > 0)){
					
					rls = itRls.next();
					if(rls.getStatus().equals("active") || rls.getStatus().equals("deprecated") || rls.getInstall().equals("installed")){
						i--;
					}
				}
			}
		}
		
		
		/* ---------------*/
		
		switch (columnIndex) {
			case 0:
				result = new ReleaseIdentifier(pkg.getName().toLowerCase(),
												pkg.getPkgid(),
												bch.getBranchid(),
												rls.getRlsid());
				break;
			case 1:
				result = pkg.getCategory().toLowerCase();
				break;
			case 2:
				result = pkg.getShortdesc();
				break;
			case 3:
				result = bch.getName().toLowerCase();
				break;
			case 4:
				result = rls.getVersion();
				break;
			case 5:
				result = rls.getStatus();
				break;
			case 6:
				if ( rls.getState().equals("stable")){
					result = "stable";
				}else{
					result = "development";
				}
				break;
			case 7:
				result = rls.getInstall();
				break;
			default:
				result = pkg.getName();
		}
		
		
		return result;
	}



	public Database getDatabase() {
		return database;
	}



	public void setDatabase(Database database) {
		this.database = database;
	}
}
